<template>
  <div class="list-container">
    <vab-query-form>
      <el-form
        ref="form"
        :model="queryForm.queryBean"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.estateType"
            placeholder="房源类型"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.building"
            placeholder="楼栋号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.estateName"
            placeholder="地产名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.projectName"
            placeholder="项目名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <!-- <el-table-column
        show-overflow-tooltip
        prop="estateId"
        label="房源ID"
        width="300"
      ></el-table-column> -->

      <el-table-column
        show-overflow-tooltip
        prop="estateName"
        label="地产名称"
        width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="projectName"
        label="项目名称"
        width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="房屋类型"
        prop="estateType"
        width="120"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="楼栋"
        prop="building"
        width="80"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="单元"
        prop="unit"
        width="80"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="房间号"
        prop="num"
        width="80"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="address"
        label="地址"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="saleStatus"
        label="销售状态"
        width="100"
      >
        <template #default="{ row }">
          <el-tag
            size="small"
            :type="
              row.saleStatus == 2
                ? 'success'
                : row.saleStatus == 3
                ? 'danger'
                : row.saleStatus == 1
                ? 'info'
                : 'warning'
            "
          >
            {{ getValue(row.saleStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-upload"
            @click="handleUpload(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-connection"
            @click="handleMapTree(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <set-map-tree ref="tree" @fetch-data="fetchData"></set-map-tree>
    <edit-house ref="edit" @fetch-data="fetchData"></edit-house>
    <el-dialog
      title="上传平面图"
      :visible.sync="uploadVisible"
      class="container"
      width="400px"
      @close="() => (uploadVisible = false)"
    >
      <el-upload
        class="upload-demo"
        action=""
        drag
        :limit="1"
        :file-list="imageUrlfileList"
        :before-upload="imageUrlBeforeUpload"
        name="fileId"
        :http-request="uploadImages"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip">
          只能上传jpg/png文件，且不超过2M
        </div>
        <!-- <el-progress
          style="width: 200px; margin-top: 8px"
          :percentage="progressPercent"
        /> -->
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="pushImage()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { fetchHouseList, uploadImage, deleteHouseEstate } from '@/api/house'
  import SetMapTree from './components/SetMapTree.vue'
  import { upload } from '@/api/upload'
  import EditHouse from './components/EditHouse.vue'

  export default {
    name: 'HouseList',
    components: { SetMapTree, EditHouse },
    data() {
      return {
        imageUrlfileList: [],
        uploadVisible: false,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
        fileId: '',
        estateId: '',
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    mounted() {},
    methods: {
      async pushImage() {
        if (this.fileId == '') {
          this.$baseMessage('请先上传平面图', 'warning')
          return
        }
        let uploadParams = {}
        uploadParams.estateId = this.estateId
        uploadParams.value = this.fileId
        uploadParams.key = 'plan'
        const { data } = await uploadImage(uploadParams)
        if (data.status === 0) {
          this.$baseMessage('提交成功', 'success')
          this.uploadVisible = false
        } else {
          this.$baseMessage(res.message, 'error')
        }
      },
      async uploadImages(param) {
        const file = param.file
        // 实例化一个formData，用来做文件上传
        console.log(param)
        const formData = new FormData()
        formData.append('fileType', 1)
        formData.append('file', file)
        const { data } = await upload(formData)
        if (data.status === 0) {
          this.$baseMessage('上传成功', 'success')
          this.fileId = data.body.fileId
        } else {
          this.$baseMessage(data.message, 'error')
        }
      },
      imageUrlBeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
      getValue(value) {
        switch (value) {
          case 1:
            return '未售'
          case 2:
            return '在售'
          case 3:
            return '已售'
        }
      },
      handleDelete(row) {
        if (row.estateId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteHouseEstate({ estateId: row.estateId })
            if (data.status == 0) {
              this.$baseMessage('删除成功！', 'success')
            } else {
              this.$baseMessage(data.message, 'error')
            }
            this.fetchData()
          })
        } else {
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      //自定义maptree
      handleMapTree(row) {
        this.$refs['tree'].showEdit(row.estateId)
      },
      handleUpload(row) {
        this.estateId = row.estateId
        // this.$refs['tree'].showEdit(row.estateId)
        this.uploadVisible = true
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        console.log(this.queryForm)
        const { data } = await fetchHouseList(this.queryForm)
        const { body } = data
        console.log(body.data)
        this.list = body.data
        this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
        // data.forEach((item, index) => {})
        this.total = body.totalSize
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
<style lang="scss" scoped>
  // .container {
  //   display: flex;
  //   justify-content: center;
  //   align-items: center;
  // }
</style>
